//
//  NBAReferee.swift
//  Sports
//
//  Created by tiger on 2017/10/16.
//  Copyright © 2017年 Meigabay. All rights reserved.
//

import UIKit

//裁判
public class NBAReferee: NBAWorker {
    
    internal weak var game: NBAGame!
    private lazy var thinkQueue: DispatchQueue = DispatchQueue(label: "RefereeBrain")

    func startGame(game: NBAGame) {
        self.game = game
        self.thinkQueue.sync {
            self.checkState()
        }
    }
    
    func checkState() {
        switch self.game!.state {  
            case .pause:
                break;
            case .playing:
                break;
            case .end:
                break;
            default:
                break;
        }
    }
    
    func prepareWork() {
        self.thinkQueue.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: 10), execute: {
            self.startWork()
        })
    }
    
    func startWork() {
        NotificationCenter.default.post(name: NSNotification.Name.NBAGameWhistle, object: NBARefereeWhistleType.jump )
        self.game?.state = .jump
        self.handJump()
    }
    
    func stopWork() {
        
    }
    
    ///处理跳球
    func handJump() {
        //查看球队双方运动员是否站好了位置
        var ready = true
        //中锋的位置
        if (self.game.home.gameDept.c.coordinate.x != 100 || self.game.home.gameDept.c.coordinate.y != 100
            && self.game.visitors.gameDept.c.coordinate.x !=  100 || self.game.visitors.gameDept.c.coordinate.y != 100) {
            ready = false
        }
        //小前锋的位置
        if (self.game.home.gameDept.sf.coordinate.x != 100 || self.game.home.gameDept.sf.coordinate.y != 100
            && self.game.visitors.gameDept.sf.coordinate.x != 100 || self.game.visitors.gameDept.sf.coordinate.y != 100) {
            ready = false
        }
        //大前锋的位置
        if (self.game.home.gameDept.pf.coordinate.x != 100 || self.game.home.gameDept.pf.coordinate.y != 100
            && self.game.visitors.gameDept.pf.coordinate.x != 100 || self.game.visitors.gameDept.pf.coordinate.y != 100) {
            ready = false
        }
        //得分后卫的位置
        if (self.game.home.gameDept.pf.coordinate.x != 100 && self.game.home.gameDept.pf.coordinate.y != 100
            && self.game.visitors.gameDept.pf.coordinate.x != 100 || self.game.visitors.gameDept.pf.coordinate.y != 100) {
            ready = false
        }
        //控球后卫的位置
        if (self.game.home.gameDept.sf.coordinate.x != 100 || self.game.home.gameDept.sf.coordinate.y != 100
            && self.game.visitors.gameDept.sf.coordinate.x != 100 || self.game.visitors.gameDept.sf.coordinate.y != 100) {
            ready = false
        }
        if ready {
            self.thinking()
        } else {
            self.handJump()
        }
    }
    
    //每隔0.1秒 做一次帧验证
    func thinking() {
        
        
        //我是谁  //我是裁判
        //我要到哪里去 //你哪里都不能去
        //我是不是要投篮 //你不能投篮
    }
    
    //暂停比赛
    public func pauseGame() {
        
    }
    
    //继续比赛
    public func resumeGame() {
        
    }
}


/// 裁判哨子类型
///
/// - start: 开始比赛
/// - end: 结束比赛
/// - pause: 暂停比赛
/// - resume: 恢复比赛
/// - jump: 跳球
internal enum NBARefereeWhistleType: UInt {
    case start = 0
    case end
    case pause
    case resume
    case jump
}

internal enum NBARefereePenaltyType: UInt {
    case start = 0
    case end
    case pause
    case resume
}

//裁判吹哨
internal class NBARefereeWhistle {
    var type: NBARefereeWhistleType!
    var penalty: NBARefereePenaltyType!
}
